package com.gjun.struts.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.gjun.struts.bean.ItemJsonBean;
import com.gjun.struts.bean.ext.Item;
import com.gjun.struts.bean.ext.Order;
import com.gjun.struts.resource.dao.IItemDao;
import com.gjun.struts.resource.dao.IOrderDao;
import com.gjun.struts.util.JSONHelper;

public class MgrItemsAction {
	
	private int pageSize = 10; 
    
    private int totalSize;
    
    private int totalPage;
    
    private int curPage;
    
    private int orderId;
    
    private int id;
    
    private int[] delId;
	
	private String result;
	
	private IItemDao itemDao;
	
	private IOrderDao orderDao;
	
	private int productId;
	private String productName;
	private double dangPrice;
	private int productNum;
	private double amount;
	
	
	
	
	/**
	 * @return the productId
	 */
	public int getProductId() {
		return productId;
	}



	/**
	 * @param productId the productId to set
	 */
	public void setProductId(int productId) {
		this.productId = productId;
	}



	/**
	 * @return the productName
	 */
	public String getProductName() {
		return productName;
	}



	/**
	 * @param productName the productName to set
	 */
	public void setProductName(String productName) {
		this.productName = productName;
	}



	/**
	 * @return the dangPrice
	 */
	public double getDangPrice() {
		return dangPrice;
	}



	/**
	 * @param dangPrice the dangPrice to set
	 */
	public void setDangPrice(double dangPrice) {
		this.dangPrice = dangPrice;
	}



	/**
	 * @return the productNum
	 */
	public int getProductNum() {
		return productNum;
	}



	/**
	 * @param productNum the productNum to set
	 */
	public void setProductNum(int productNum) {
		this.productNum = productNum;
	}



	/**
	 * @return the amount
	 */
	public double getAmount() {
		return amount;
	}



	/**
	 * @param amount the amount to set
	 */
	public void setAmount(double amount) {
		this.amount = amount;
	}



	/**
	 * @return the orderDao
	 */
	public IOrderDao getOrderDao() {
		return orderDao;
	}



	/**
	 * @param orderDao the orderDao to set
	 */
	public void setOrderDao(IOrderDao orderDao) {
		this.orderDao = orderDao;
	}



	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}



	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}



	/**
	 * @return the totalSize
	 */
	public int getTotalSize() {
		return totalSize;
	}



	/**
	 * @param totalSize the totalSize to set
	 */
	public void setTotalSize(int totalSize) {
		this.totalSize = totalSize;
	}



	/**
	 * @return the totalPage
	 */
	public int getTotalPage() {
		return totalPage;
	}



	/**
	 * @param totalPage the totalPage to set
	 */
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}



	/**
	 * @return the curPage
	 */
	public int getCurPage() {
		return curPage;
	}



	/**
	 * @param curPage the curPage to set
	 */
	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}






	/**
	 * @return the orderId
	 */
	public int getOrderId() {
		return orderId;
	}



	/**
	 * @param orderId the orderId to set
	 */
	public void setOrderId(int orderId) {
		this.orderId = orderId;
	}



	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}



	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}



	/**
	 * @return the delId
	 */
	public int[] getDelId() {
		return delId;
	}



	/**
	 * @param delId the delId to set
	 */
	public void setDelId(int[] delId) {
		this.delId = delId;
	}



	/**
	 * @return the result
	 */
	public String getResult() {
		return result;
	}



	/**
	 * @param result the result to set
	 */
	public void setResult(String result) {
		this.result = result;
	}



	/**
	 * @return the itemDao
	 */
	public IItemDao getItemDao() {
		return itemDao;
	}



	/**
	 * @param itemDao the itemDao to set
	 */
	public void setItemDao(IItemDao itemDao) {
		this.itemDao = itemDao;
	}



	public String loadItemsByOrderId(){
		orderId = (Integer)ServletActionContext.getRequest().getSession().getAttribute("orderId");
		totalSize = itemDao.getItemByOrderId(orderId).size();
		totalPage = (totalSize + pageSize - 1) /pageSize;
		if(curPage == 0)
			curPage = 1;
		int start = (curPage - 1)*pageSize;
		List<Item> items = itemDao.getItemByOrderId(orderId,start,pageSize);
		List<ItemJsonBean> jsonBeans = new ArrayList();
		for (Item item : items) {
			ItemJsonBean bean = new ItemJsonBean(item);
			jsonBeans.add(bean);
		}
		result = JSONHelper.itemToJson(jsonBeans);
		return "success";
	}
	
	public String loadItemsData(){
		
		return "success";
	}
	
	public String delItemsById(){
		Item item = itemDao.getItemById(id);
		itemDao.deleteItemById(item);
		result = "deleteItemSuccess";
		return "success";
	}
	
	public String loadItemsEditById(){
		Item item = itemDao.getItemById(id);
		ServletActionContext.getRequest().setAttribute("item", item);
		result = "to_item_page";
		return "success";
	}
	
	public String update(){
		Item item = itemDao.getItemById(id);
		amount = productNum * dangPrice;
		item.setProductNum(productNum);
		item.setDangPrice(dangPrice);
		Order order = item.getOrder();
		order.setTotalPrice(order.getTotalPrice()-item.getAmount()+amount);
		item.setAmount(amount);
		item.setOrder(order);
		itemDao.updateItem(item);
		result = "updateSuccess";
		return "success";
	}
}
